package com.eb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eb.context.BaseContext;
import com.eb.domain.Announcement;
import com.eb.dto.AnnouncementPageQueryDTO;
import com.eb.mapper.IAnnouncementMapper;
import com.eb.result.PageResult;
import com.eb.service.AnnouncementService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<IAnnouncementMapper, Announcement> implements AnnouncementService {

    /**
     * 分页查询通知
     * @param announcementPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(AnnouncementPageQueryDTO announcementPageQueryDTO) {
        // 1.准备分页条件
        int pageNo = announcementPageQueryDTO.getPageNo();
        int pageSize = announcementPageQueryDTO.getPageSize();
        Page<Announcement> page = Page.of(pageNo, pageSize);
        // 2.排序条件 - 按照时间降序排列
        page.addOrder(new OrderItem("create_time", false));
        // 2.根据当前用户id查询
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<Announcement>()
                .eq("user_id", BaseContext.getCurrentId());
        // 3.分页查询
        Page<Announcement> p = page(page, queryWrapper);
        // 4.解析
        long total = p.getTotal();
        List<Announcement> records = page.getRecords();
        return new PageResult(total, records);
    }
}
